package zozopo.warehouse;

import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.PurchaseOS;
import zozopo.warehouse.operations.PurchaseTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ExportGoodsInfoAction extends ActionSupport implements Action,SessionAware,ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.GOODS.QUERY";
	
	//传入参数
	private String begindate;
	private String enddate;
	
	private int number=0;
	
	//分页参数
	
	//传出参数
	
	private HttpServletResponse response;
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
	}
	private Map<Object, Object> session;
	public void setSession(Map arg0) { 
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		Date dt=new Date();
	    SimpleDateFormat matter1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    // System.out.println(matter1.format(dt)); 
	    String dateStr = "";
		
		GoodsOS goodsOS = new GoodsOS();
		//UserOS userOS = new UserOS();
		List<GoodsTB> goodsList;
		
		
		if(begindate!=null && enddate!=null){
			if(!begindate.equals("") && !enddate.equals("")){
				goodsList = goodsOS.select(begindate, enddate);
				dateStr = begindate + " - "+enddate;
			}else{
				goodsList =goodsOS.selectAll();
				dateStr = matter1.format(dt);
			}
		}else{
			goodsList =goodsOS.selectAll();
			dateStr = matter1.format(dt);
		}
		
		
		
		PurchaseOS purchaseOS = new PurchaseOS(goodsOS.getConn());
		
		Map<Integer,List<PurchaseTB>> warehouseInfoMapList = purchaseOS.getWarehouseInfoMapList(goodsList);;
		goodsOS.releaseConnection();
		
		

		try {
			goodsOS.write2Excel("Goods List"+dateStr, response, goodsList,warehouseInfoMapList,number);
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return NONE;
		
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有产品查询权限.");
			return;
		}
	}
	public static String getLimitedcode() {
		return limitedCode;
	}
	public String getBegindate() {
		return begindate;
	}
	public void setBegindate(String begindate) {
		this.begindate = begindate;
	}
	public String getEnddate() {
		return enddate;
	}
	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
}
